package com.test.ddaidai;

import java.util.Map;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.test.ddaidai.gesture.Base64;
import com.test.ddaidai.gesture.RSACoder;
import com.test.widget.LockPatternUtils;

public class MyApp extends Application {
	private static MyApp mInstance;
	private LockPatternUtils mLockPatternUtils;
	public static final String DDAIDAI_SP = "ddaidai_sp" ;

	public static MyApp getInstance() {
		return mInstance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
 		mInstance = this;
		mLockPatternUtils = new LockPatternUtils(this);
//		SharedPreferences sp = getSharedPreferences(LockPatternUtils.LOCK_PATTERN_PREFRENCE, Context.MODE_PRIVATE);
		SharedPreferences sp = getSharedPreferences(DDAIDAI_SP,Context.MODE_PRIVATE);
		if(!sp.contains(LockPatternUtils.LOCK_PATTERN_PRIVATE) && !sp.contains(LockPatternUtils.LOCK_PATTERN_PUBLIC) ){
			try {
				//初始化密钥  
		        //生成密钥对  
		        Map<String,Object> keyMap=RSACoder.initKey();  
		        //公钥  
		        byte[] publicKey=RSACoder.getPublicKey(keyMap);
		        //私钥  
		        byte[] privateKey=RSACoder.getPrivateKey(keyMap);
		        Editor editor = sp.edit();
		        editor.putString(LockPatternUtils.LOCK_PATTERN_PRIVATE, Base64.encodeBase64String(privateKey));
		        editor.putString(LockPatternUtils.LOCK_PATTERN_PUBLIC, Base64.encodeBase64String(publicKey));
		        editor.commit();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

	public LockPatternUtils getLockPatternUtils() {
		return mLockPatternUtils;
	}

}
